package com.mai4j.maiadmin.controller.api.system.localisation;

import com.mai4j.maiadmin.misc.StructMapper;
import com.mai4j.maiadmin.model.RespCode;
import com.mai4j.maiadmin.model.dto.LanguageDTO;
import com.mai4j.maiadmin.model.dto.PageDTO;
import com.mai4j.maiadmin.model.http.LanguageDeleteReq;
import com.mai4j.maiadmin.model.http.LanguageGetReq;
import com.mai4j.maiadmin.model.http.LanguageSaveOrUpdateReq;
import com.mai4j.maiadmin.model.http.datatables.Datatable;
import com.mai4j.maiadmin.model.http.datatables.response.DatatableResp;
import com.mai4j.maiadmin.model.http.response.*;
import com.mai4j.maiadmin.service.LanguageService;
import com.mai4j.maiadmin.support.I18nProvider;
import com.mai4j.maiadmin.support.security.aop.annotation.RequiresAuthentication;
import com.mai4j.maiadmin.support.web.binding.ReqParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 微信公众号请关注: 开源螺丝钉
 * <br>
 * 码云请关注: <a href="https://gitee.com/xiyoufang">https://gitee.com/xiyoufang</a>
 * <br>
 * 哔哩哔哩请关注: <a href="https://space.bilibili.com/438927834">https://space.bilibili.com/438927834</a>
 *
 * @author xiyoufang
 */
@RequiresAuthentication
@RestController
@RequestMapping("/api/system/localisation/language")
public class ApiLanguageController {

    private final LanguageService languageService;

    public ApiLanguageController(LanguageService languageService) {
        this.languageService = languageService;
    }

    @PostMapping("/list")
    public ResponseEntity<LanguageListResp> list() {
        return ResponseEntity.ok(new LanguageListResp()
                .setLanguages(languageService.list()
                        .stream().filter(l -> Objects.equals(l.getStatus(), (byte) 1))
                        .map(StructMapper.INSTANCE::languageDTOToLanguageListRespLanguage)
                        .collect(Collectors.toList()))
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }

    @PostMapping("/get")
    public ResponseEntity<LanguageGetResp> get(@RequestBody @Valid LanguageGetReq languageGetReq) {
        LanguageDTO languageDTO = languageService.get(Long.parseLong(languageGetReq.getLanguageId()));
        return ResponseEntity.ok(new LanguageGetResp()
                .setLanguage(StructMapper.INSTANCE.languageDTOToLanguageVO(languageDTO))
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }

    @PostMapping("/saveOrUpdate")
    public ResponseEntity<LanguageSaveOrUpdateResp> saveOrUpdate(@RequestBody @Validated LanguageSaveOrUpdateReq languageSaveOrUpdateReq) {
        LanguageDTO languageDTO = languageService.saveOrUpdate(StructMapper.INSTANCE.languageSaveOrUpdateReqToLanguageDTO(languageSaveOrUpdateReq));
        return ResponseEntity.ok(new LanguageSaveOrUpdateResp()
                .setLanguageId(String.valueOf(languageDTO.getLanguageId()))
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }

    @PostMapping("/delete")
    public ResponseEntity<SimpleResp> delete(@RequestBody @Validated LanguageDeleteReq languageDeleteReq) {
        languageService.delete(Long.parseLong(languageDeleteReq.getLanguageId()));
        return ResponseEntity.ok(new SimpleResp()
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }

    /**
     * 分页
     *
     * @param datatable datatable
     */
    @PostMapping("/datatable")
    public ResponseEntity<DatatableResp> datatable(@ReqParam Datatable datatable) {
        PageDTO<LanguageDTO> pageDTO = languageService.paginate(datatable);
        return ResponseEntity.ok(new DatatableResp()
                .setDraw(datatable.getDraw())
                .setRecordsFiltered(pageDTO.getTotalRow())
                .setRecordsTotal(pageDTO.getTotalRow())
                .setData(pageDTO.getList()
                        .stream()
                        .map(StructMapper.INSTANCE::languageDTOToLanguageVO)
                        .collect(Collectors.toList())));
    }

    /**
     * 获取 locales 列表
     *
     * @return locales
     */
    @PostMapping("/locales")
    public ResponseEntity<LocaleResp> locales() {
        return ResponseEntity.ok(
                new LocaleResp().setCode(RespCode.OK.getCode())
                        .setMessage(I18nProvider.get("text.successfully"))
                        .setLocales(Arrays.stream(Locale.getAvailableLocales())
                                .map(Locale::toLanguageTag)
                                .filter(StringUtils::isNotBlank)
                                .sorted()
                                .collect(Collectors.toList())));
    }

}
